#include "mainwin.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QString>
#include <QStringList>
#include <QGroupBox>

MainWin::MainWin(QWidget *parent) : QWidget(parent),bOpen(false)
{
    bitHandle  = 0x00;
    groupHandle = 0x00;
    m_ioCtrl = new IoCtrl;
    createWin();
    setConnect();
}

void MainWin::createWin()
{
    QLabel *pComLbl = new QLabel("COM:");
    m_pComEdit      = new QLineEdit;
    m_pOpenCom      = new QPushButton("打开串口");
    QHBoxLayout *pComHBL = new QHBoxLayout;
    pComHBL->addWidget(pComLbl);
    pComHBL->addWidget(m_pComEdit);
    pComHBL->addWidget(m_pOpenCom);

    m_pBitOpenBtn  = new QPushButton("位开");
    m_pBitCloseBtn = new QPushButton("位关");

    m_pBitBarrier0Radio = new QRadioButton("继电器0");
    m_pBitBarrier1Radio = new QRadioButton("继电器1");
    m_pBitBarrier2Radio = new QRadioButton("继电器2");
    m_pBitBarrier3Radio = new QRadioButton("继电器3");

    QHBoxLayout *pBitRadioHBL = new QHBoxLayout;
    pBitRadioHBL->addWidget(m_pBitBarrier0Radio);
    pBitRadioHBL->addWidget(m_pBitBarrier1Radio);
    pBitRadioHBL->addWidget(m_pBitBarrier2Radio);
    pBitRadioHBL->addWidget(m_pBitBarrier3Radio);

    QGroupBox *pGroupBoxBit = new QGroupBox("位开、关");
    pGroupBoxBit->setLayout(pBitRadioHBL);

    QHBoxLayout *pBitBtn = new QHBoxLayout;
    pBitBtn->addWidget(pGroupBoxBit);
    pBitBtn->addWidget(m_pBitOpenBtn);
    pBitBtn->addWidget(m_pBitCloseBtn);

    m_pGroupOpenBtn = new QPushButton("组开");
    m_pGroupCloseBtn = new QPushButton("组关");

    m_pGroupBarrier0Radio = new QCheckBox("继电器0");
    m_pGroupBarrier1Radio = new QCheckBox("继电器1");
    m_pGroupBarrier2Radio = new QCheckBox("继电器2");
    m_pGroupBarrier3Radio = new QCheckBox("继电器3");

    QHBoxLayout *pGroupRadioHBL = new QHBoxLayout;
    pGroupRadioHBL->addWidget(m_pGroupBarrier0Radio);
    pGroupRadioHBL->addWidget(m_pGroupBarrier1Radio);
    pGroupRadioHBL->addWidget(m_pGroupBarrier2Radio);
    pGroupRadioHBL->addWidget(m_pGroupBarrier3Radio);

    QGroupBox *pGroupBoxGroup = new QGroupBox("组开、关");
    pGroupBoxGroup->setLayout(pGroupRadioHBL);

    QHBoxLayout *pGroupBtn = new QHBoxLayout;
    pGroupBtn->addWidget(pGroupBoxGroup);
    pGroupBtn->addWidget(m_pGroupOpenBtn);
    pGroupBtn->addWidget(m_pGroupCloseBtn);

    m_pAllOpenBtn = new QPushButton("全开");
    m_pAllCloseBtn = new QPushButton("全关");

    QHBoxLayout *pAllBtn  = new QHBoxLayout;
    pAllBtn->addWidget(m_pAllOpenBtn);
    pAllBtn->addWidget(m_pAllCloseBtn);

    m_pResultLbl = new QLabel;

    QVBoxLayout *pMainWindow = new QVBoxLayout;
    pMainWindow->addLayout(pComHBL);
    pMainWindow->addLayout(pBitBtn);
    pMainWindow->addLayout(pGroupBtn);
    pMainWindow->addLayout(pAllBtn );
    pMainWindow->addWidget(m_pResultLbl);

    this->setLayout(pMainWindow);
}

void MainWin::setConnect()
{
    connect(m_pOpenCom,      SIGNAL(clicked()),this,SLOT(slotOpenCom()));
    connect(m_pAllOpenBtn,   SIGNAL(clicked()),this,SLOT(slotAllOpenBtn()));
    connect(m_pAllCloseBtn,  SIGNAL(clicked()),this,SLOT(slotAllCloseBtn()));
    connect(m_pBitOpenBtn,   SIGNAL(clicked()),this,SLOT(slotBitOpenBtn()));
    connect(m_pBitCloseBtn,  SIGNAL(clicked()),this,SLOT(slotBitCloseBtn()));
    connect(m_pGroupOpenBtn, SIGNAL(clicked()),this,SLOT(slotGroupOpenBtn()));
    connect(m_pGroupCloseBtn,SIGNAL(clicked()),this,SLOT(slotGroupCloseBtn()));

    connect(m_ioCtrl,SIGNAL(sigMsg(QString)),this,SLOT(slotErrMsg(QString)));
}

void MainWin::returnBitNum()
{
    if(m_pBitBarrier0Radio->isChecked())
    {
        bitHandle = 0x04;
    }
    else if(m_pBitBarrier1Radio->isChecked())
    {
        bitHandle = 0x03;
    }
    else if(m_pBitBarrier2Radio->isChecked())
    {
        bitHandle = 0x02;
    }
    else if(m_pBitBarrier3Radio->isChecked())
    {
        bitHandle = 0x01;
    }
}

void MainWin::returnGroupNum()
{
    if(m_pGroupBarrier0Radio->isChecked())
    {
        groupHandle |= 0x08;
        qDebug() << "m_pBitBarrier0Radio" << groupHandle;
    }
    if(m_pGroupBarrier1Radio->isChecked())
    {
        groupHandle |= 0x04;
        qDebug() << "m_pBitBarrier1Radio" << groupHandle;
    }
    if(m_pGroupBarrier2Radio->isChecked())
    {
        groupHandle |= 0x02;
        qDebug() << "m_pBitBarrier2Radio" << groupHandle;
    }
    if(m_pGroupBarrier3Radio->isChecked())
    {
        groupHandle |= 0x01;
        qDebug() << "m_pBitBarrier3Radio" << groupHandle;
    }
}

void MainWin::slotOpenCom()
{
    QString strCom = m_pComEdit->text().trimmed();
    if(strCom.isEmpty())
    {
        m_pResultLbl->setText("com 不能为空");
        return;
    }
    else
    {
        if(m_ioCtrl->open(strCom))
        {
            bOpen = true;
            m_pResultLbl->setText("串口打开成功");
        }
        else
        {
            m_pResultLbl->setText("串口打开失败");
        }
    }
}

void MainWin::slotBitOpenBtn()
{
    if(!bOpen)
    {
        m_pResultLbl->setText("串口打开失败");
        return;
    }
    returnBitNum();
    qDebug() << bitHandle;
    m_ioCtrl->sendcommand(WAY::Bit,true,bitHandle);
    bitHandle = 0x00;
}

void MainWin::slotBitCloseBtn()
{
    if(!bOpen)
    {
        m_pResultLbl->setText("串口打开失败");
        return;
    }
    returnBitNum();
    qDebug() << bitHandle;
    m_ioCtrl->sendcommand(WAY::Bit,false,bitHandle);
    bitHandle = 0x00;
}

void MainWin::slotGroupOpenBtn()
{
    if(!bOpen)
    {
        m_pResultLbl->setText("串口打开失败");
        return;
    }
    returnGroupNum();
    qDebug() << groupHandle;
    m_ioCtrl->sendcommand(WAY::Group,true,groupHandle);
    groupHandle = 0x00;
}

void MainWin::slotGroupCloseBtn()
{
    if(!bOpen)
    {
        m_pResultLbl->setText("串口打开失败");
        return;
    }
    returnGroupNum();
    qDebug() << groupHandle;
    m_ioCtrl->sendcommand(WAY::Group,false,groupHandle);
    groupHandle = 0x00;
}

void MainWin::slotAllOpenBtn()
{
    if(!bOpen)
    {
        m_pResultLbl->setText("串口打开失败");
        return;
    }
    m_ioCtrl->sendcommand(WAY::All,true,0x00);
}

void MainWin::slotAllCloseBtn()
{
    if(!bOpen)
    {
        m_pResultLbl->setText("串口打开失败");
        return;
    }
    m_ioCtrl->sendcommand(WAY::All,false,0x00);
}

void MainWin::slotErrMsg(const QString& strErr)
{
    m_pResultLbl->clear();
    m_pResultLbl->setText(strErr);
}
